#! /usr/bin/env python
# -*- coding: utf-8 -*-

# @author: xmj
# @date: 2023/5/3

import logging
import logging.handlers
from common.get_path import log_path
from common.utils import get_str_time


def init_logging(report_time):
    """
    初始化日志文件
    :param report_time: 扫描开始时间
    :return:
    """
    log_file_name = log_path + '\\' + 'endpoint_scan_' + report_time + '.log'
    logger = logging.getLogger()
    logger.setLevel(20)
    # sh = logging.StreamHandler()
    # 此处需要添加utf-8编码，不然日志写入中文会报错
    file_log = logging.handlers.TimedRotatingFileHandler(log_file_name, 'MIDNIGHT', 1, 0, encoding='utf-8')
    fmt = '[%(asctime)s] [%(levelname)-7s] %(thread)d - %(message)s'
    formatter = logging.Formatter(fmt)
    # sh.setFormatter(formatter)
    file_log.setFormatter(formatter)
    # logger.addHandler(sh)
    logger.addHandler(file_log)
    logging.info("Current log level is : %s", logging.getLevelName(logger.getEffectiveLevel()))
